using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using GameCore;
using GameObject;
using ResourceManager;
using HandlingInput;

namespace ManagerGameState
{
    public class GameMenu : GameState
    {
        public GameMenu()
        {
            this.GAME_STATE_DEF = (int)GSID.GameSateID.GAME_STATE_MENU;
        }

        List< Button> l_bt;
        int count;
        int count2;
        int margin_top;
        int margin_left;
        Sprite background;
        protected override void Enter(GameTime gameTime)
        {
            l_bt = new List<Button>();

            ButtonManager btMan = new ButtonManager();
            btMan.InitPrototype();
            
            for (int i = 0; i < 3; i++)
            {
                Button bt = (Button)btMan.CreateObject(i);
                bt.Hover += new Window.GetEvent(Hover);
                bt.Normal += new Window.GetEvent(Button_Normal);
                l_bt.Add(bt);
            }

            l_bt[2].Enter+=new Window.GetEvent(Button_Exit_Enter);
            l_bt[0].Enter += new Window.GetEvent(GameMenu_Enter);

            count = 20;
            count2 = 0;
            margin_top = 50;
            margin_left = 100;

            Texture2D[] ttx = new Texture2D[1];
            ttx[0] = GlobalDefine.Content.Load<Texture2D>(@"Pictures\wall");
            background = new Sprite(0,0,0,0,ttx);
        }

        void GameMenu_Enter(Window window, GameState gs)
        {
            GameStateControl.SetNextGameState((int)GSID.GameSateID.GAME_STATE_MAIN);
        }
        protected override void Update(GameTime gameTime)
        {
            count2++;
            count2 %= 3;
            
            for (int i = 0; i < 3; i++)
            {
                if (l_bt[i].Y != i * 50 + margin_top+10)
                {
                    l_bt[i].Update(gameTime, margin_left,
                        count*i * 50 + margin_top + count * 10, 150, 45);
                }
                else
                    l_bt[i].Update(gameTime);
                
            }
            if (count > 1 && (count2 == 0))
                count--;

            background.Update(gameTime, 0, 0, 0, GlobalDefine.Window.ClientBounds.Width,
                GlobalDefine.Window.ClientBounds.Height);
        }
        protected override void Render(GameTime gameTime, SpriteBatch g_DC)
        {
            g_DC.GraphicsDevice.Clear(Color.Black);
            background.Draw(gameTime, g_DC);
            for (int i = 0; i < 3; i++)
                l_bt[i].Draw(gameTime,g_DC);
        }
        protected override void RenderEnter(GameTime gameTime, SpriteBatch g_DC)
        {
            g_DC.GraphicsDevice.Clear(Color.Wheat);
        }
        // Event of Button
        private void Hover(Window button, GameState gs)
        {
            if (button.Tag == null)
            {
                button.Width += 30;
                button.X += 30;
                
                button.Tag = 1;
            }
          /*  else if ((int)button.Tag < 5)
            {
                button.Width += 30;
                button.X += 30;

                button.Tag = (int)button.Tag+1;
            }*/
                
        }
        private void Button_Normal(Window button, GameState gs)
        {

            if (button.Tag != null)
            {
                button.Width -= 30*((int)button.Tag);
                button.X -= 30 * ((int)button.Tag);
                button.Tag = null;
            }
        }
        private void Button_Exit_Enter(Window button, GameState gs)
        {
            GameStateControl.SetNextGameState((int)GSID.GameSateID.GAME_STATE_EXIT);
        }
    }
}
